package com.ocom.amqp.configuration;

import com.ocom.amqp.builder.CurrentLimitingQueueBuilder;
import com.ocom.amqp.builder.DelayQueueBuilder;
import com.ocom.amqp.core.AmqpBasicBeanProcessor;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Configuration
@ConditionalOnBean({AmqpBasicBeanProcessor.class})
public class AmqpBasicAutoConfiguration {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Resource
    private RabbitAdmin rabbitAdmin;


    @Bean
    public RabbitTemplate rabbitTemplate(CachingConnectionFactory cachingConnectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);
        return rabbitTemplate;
    }

    @Bean
    public RabbitAdmin rabbitAdmin() {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(this.rabbitTemplate);
        rabbitAdmin.setAutoStartup(true);
        return rabbitAdmin;
    }

    @Bean
    public DelayQueueBuilder delayQueueBuilder() {
        DelayQueueBuilder delayQueueBuilder = new DelayQueueBuilder(this.rabbitAdmin);
        return delayQueueBuilder;
    }

    @Bean
    public CurrentLimitingQueueBuilder currentLimitingQueueBuilder() {
        CurrentLimitingQueueBuilder currentLimitingQueueBuilder = new CurrentLimitingQueueBuilder(this.rabbitAdmin);
        return currentLimitingQueueBuilder;
    }

}
